<meta charset="utf-8">
<?php
//判断cookie是否存在，如果存在获取
$username="";
$password="";
if(isset($_COOKIE["username"])){
	$username=$_COOKIE["username"];
}
if(isset($_COOKIE["password"])){
	$password=$_COOKIE["password"];
}
?>
<div style="border: 2px solid #ABABAB;width: 400px;margin: 0 auto;background:#EEEEEE;margin-top: 300px;border-radius:5px;">
<form method="POST" action="login.php">
	<div style="text-align: center;">
	<h3>SUPPER课程表</h3>
	
	<br/>
	用户名：<input id="username" type="text" name="username" value="<?php echo $username; ?>"><br/><br/>
	密&nbsp;&nbsp;&nbsp;码 &nbsp;: <input id="password" type="password" name="password" value="<?php echo $password;?>"><br/><br/>
	验证码：<input type="text" name="code" id="code" style="width: 120px; padding-top:6px;">&nbsp;<img src="code.php"><br/><br/>
	
	<input type="button" name="zhuce" value="注册" onclick="window.location.href='Registration.php'">&nbsp;&nbsp;&nbsp;&nbsp;
	<input type="submit" name="submit" value="登录">
	&nbsp;&nbsp;&nbsp;&nbsp;
<input type="button" name="Submit" value="返回" onclick="javascript:window.history.back(-1);"> 
	</div>
</form>
</div>
<?php
if(isset($_POST["submit"])&&$_POST["submit"]=="登录"){
		session_start();
 		$_SESSION['username']=$_POST['username'];
 		$_SESSION['password']=$_POST['password'];
	 if($_SESSION['username']=="root"&&$_SESSION['password']=="root"){
	         // echo "管理员";
	         $username="管理员";
	         setcookie("username",$username,time()+60*60);
	         header("refresh:3;url=adminindex.php");
       }else{
             // echo "普通用户";

		    $username=$_POST["username"];
			$password=$_POST["password"];
			$code=$_POST["code"];
			
			if ($code!=$_SESSION["code"]) 
			{
				echo "<script>alert('验证码不正确');location.href='login.php';</script>";
			}
			$selectusersql="select * from users where username='$username' and password='$password'";
			include_once("database.php");
			$result=@mysql_query($selectusersql) or die("登录失败".mysql_error()."<br/>");
			if(mysql_num_rows($result)>0){
				setcookie("username",$username,time()+60*60);
				header("refresh:3;url=index.php");
			}else{
				echo "<script>alert(\"用户名或密码错误\");</script>";
			}
       }
}
?>